/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package shared;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import shared.FileDownloadListener;

/**
 *
 * @author eric
 */
public class FileDownloadManager extends FileTransferManager {

    private byte[] fileData;
    private InputStream stream;
    private List<FileDownloadListener> listeners;

    public FileDownloadManager(FileInfo fileInfo, InputStream stream) {
        super();
        listeners = new ArrayList<FileDownloadListener>();

        this.fileInfo = fileInfo;

        if (fileInfo.getFileSize() > Integer.MAX_VALUE) {
            throw new IllegalArgumentException();
        }
        int size = fileInfo.getFileSize().intValue();
        this.stream = stream;
        fileData = new byte[size];
        if (size < MAX_CHUNK_SIZE) {
            chunkSize = size;
        } else {
            chunkSize = MAX_CHUNK_SIZE;
        }


    }

    /*
    public boolean isInProgress()
    {
    return isInProgress;
    }
     *
     */

 
    public void run() {
        setStatus(TransferStatus.IN_PROGRESS);
        boolean readMore = true;
        // isInProgress=true;
        while (getStatus() == TransferStatus.IN_PROGRESS) {
            try {
                int bytesRead = 0;
                int offset = totalBytesTransferred;
                int readAmount = chunkSize;
                if (fileData.length - totalBytesTransferred < chunkSize)
                {
                    offset = fileData.length - (fileData.length%chunkSize);
                    readAmount = fileData.length%chunkSize;
                }
                bytesRead = stream.read(fileData, offset, readAmount);
                if (bytesRead == -1) {
                    {
                        if (totalBytesTransferred == fileData.length)
                             setStatus(TransferStatus.COMPLETE);
                        else
                            setStatus(TransferStatus.STOPPED);
                    }
                   
                } else {
                    totalBytesTransferred += bytesRead;
                    if (totalBytesTransferred >= fileData.length)
                        setStatus(TransferStatus.COMPLETE);
                }
                /*
                int bytesRead=0;
                if ( successfullChunks*chunkSize <= fileData.length)
                {
                bytesRead = stream.read(fileData, successfullChunks*chunkSize, chunkSize);
                successfullChunks++;
                }
                if (bytesRead !=-1)
                totalBytesTransferred += bytesRead;
                if (bytesRead < chunkSize ) {
                // isInProgress=false;
                readMore = false;
                }
                 *
                 */
                
            } catch (IOException ex) {
                if (totalBytesTransferred < fileInfo.getFileSize()) {
                    setStatus(TransferStatus.STOPPED);
                } else {
                    setStatus(TransferStatus.COMPLETE);
                }
            }
        }
         if (getStatus() == TransferStatus.COMPLETE);
                    transferComplete();
    }
    public void setInputStream(InputStream s)
    {
        stream = s;
    }
    public byte[] getFileData() {
        return fileData;
    }
    public void addListener(FileDownloadListener l)
    {
        listeners.add(l);
    }
    public void transferComplete()
    {
        for(FileDownloadListener l: listeners)
            l.downloadComplete(this);
    }

    /**
     * @return the fileInfo
     */
 
}
